import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Detail, Brand, Order, MarketDepartmentList, RegionList, DealerList} from './DTO';
const baseUrl = '/parts/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '备件货运委托书'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const shipDesc = formatMessage({
    id: 'api.message.shipDesc',
    defaultMessage: '生成出运通知书'
});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

const orderDesc = formatMessage({
    id: 'api.message.orderDesc',
    defaultMessage: '查询销售订单'
});

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partShipOrders?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};


// 查询详情
export const getDetail = id => request(`${baseUrl}/partShipOrders/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 新增
export const addInfo = data => request(`${baseUrl}/partShipOrders`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/partShipOrders/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));


import {baseDataStatus} from '../Enum';
// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};

// 获取备件发运单
export const getSaleOrders = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partShipments/forPartShipOrder?${queryString}`, null, Order)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, orderDesc));
};

export const getSaleOrderSimple = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partShipments/forPartShipOrder?${queryString}`, null, Order)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, orderDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代信息'
});

const getListTreeDesc = formatMessage({
    id: 'api.getListTreeDesc',
    defaultMessage: '查询大区树'
});

const getRegionDesc = formatMessage({
    id: 'api.getRegionDesc',
    defaultMessage: '查询国际区域列表'
});

// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 根据国际区域查询大区
export const getMarketDepartments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketDepartments/forSubDealer?${queryString}`, null, MarketDepartmentList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListTreeDesc));
};

// 查询国际区域列表
export const getRegions = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/areaTileds/forSubDealer?${queryString}`, null, RegionList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getRegionDesc));
};

//根据条件查询总代子公司
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

//根据条件查询备件货代信息及联系人信息
export const getCarriers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/carriers/forPartShipOrder?${queryString}`, null, DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
